home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Util / Quit.c < prev    next >
Text File  |  1996-04-10  |  3KB  |  146 lines

  1. /*****
  2.  *
  3.  *    Quit.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #include "compiler_stuff.h"
  18. #include "globals.h"
  19.  
  20. #include "EventUtil.h"
  21. #include "LogUtil.h"
  22. #include "MemoryUtil.h"
  23. #include "ProcessUtil.h"
  24. #include "WindowInt.h"
  25.  
  26. #include "Quit.h"
  27.  
  28.  
  29. /***  INTERFACE  ***/
  30.  
  31. /* Handle an interface or scripting call to quit the applciation */
  32. void
  33. doQuitApp ( void )
  34. {
  35.     /* set application to quit */
  36.     gQuit = true;
  37. } /* doQuitApp */
  38.  
  39.  
  40. /***  FUNCTIONS  ***/
  41. #pragma mark -
  42.  
  43. /* Confirm that the application can quit, prompt the user if necessary.
  44.     Clean up, if the application can quit.
  45.     Return true if application is set to quit, false if it can't quit. */
  46. Boolean
  47. QuitPrepare ( Boolean allowUserInteract )
  48. {
  49.     Boolean            memoryLow;
  50.     Boolean            gotEvent;
  51.     EventRecord        theEvent;
  52.     Boolean            success;
  53.     
  54.     gQuit = true;
  55.     
  56.     /* check for emergency memory store. If it isn't available, memory is low */
  57.     memoryLow = !IsEmergencyMemAvail ();
  58.     
  59.     /* Should add check for outstanding events and handle them as appropriate.
  60.         Especially important is to check for any queued high level events. */
  61.     if ( !memoryLow )
  62.     {
  63.         /* check for outstanding events and handle them as appropriate */
  64.         do
  65.         {
  66.             gotEvent = WaitNextEvent ( highLevelEventMask, &theEvent, 0, NULL );
  67.             if ( gotEvent )
  68.             {
  69.                 /* • Need to figure out a good way to distinguish between idle time
  70.                     quits and others. So, if quitting on idle and an event is
  71.                     found gQuit will be reset to false. Otherwise, if quitting
  72.                     normally and an event is found gQuit will be left alone. */
  73.                 switch ( theEvent.what )
  74.                 {
  75.                     case kHighLevelEvent :
  76.                         doHighLevelEvent ( &theEvent );
  77.                         break;
  78.                 }
  79.             }
  80.         } while ( gotEvent == true );
  81.     }
  82.         
  83.     #if kCompileWithForeground
  84.     /* get rid of document windows */
  85.     success = WindowCloseAll ( (!memoryLow) && allowUserInteract );
  86.     
  87.     if ( success || !allowUserInteract || memoryLow )
  88.     #endif    /* kCompileWithForeground */
  89.     {
  90.         #if kCompileWithThreadsOptional
  91.         if ( gHasThreadMgr )
  92.         #endif
  93.         {
  94.             /* while there remain other threads, let them finish before quitting */
  95.             ThreadFinishAllSubThreads ();
  96.         }
  97.         
  98.         success = CustomQuit ( allowUserInteract );
  99.         
  100.         /* close up the log file */
  101.         LogQuit ();
  102.         
  103.         /* can quit if successful or user is not allowed to interact */
  104.         gQuit = ( success || !allowUserInteract || memoryLow );
  105.     }
  106.     #if kCompileWithForeground
  107.     else
  108.     {
  109.         gQuit = false;
  110.     }
  111.     #endif
  112.     
  113.     return gQuit;
  114. } /* QuitPrepare */
  115.  
  116.  
  117. /* force the application to quit, doing necessary cleanup. */
  118. p_export
  119. void
  120. ForceQuit ( void )
  121. {
  122.     /* quit with no user interaction */
  123.     QuitPrepare ( false );
  124.     
  125.     ExitToShell ();
  126. } /* ForceQuit */
  127.  
  128.  
  129. /**  Utilities  **/
  130. #pragma mark -
  131.  
  132. #if kCompileWithQuitOnLongIdle
  133. p_export
  134. void
  135. ResetQuitIdleTimer ( void )
  136. {
  137.     if ( gDoIdleQuit )
  138.     {
  139.         gTimeLastAction = TickCount ();
  140.     }
  141. } /* ResetQuitIdleTimer */
  142. #endif
  143.  
  144.  
  145. /*****  EOF  *****/
  146.